package top.krwlc.server.controller;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

/**
 * krwlc
 * top.krwlc.server.controller
 *
 * @author Lanscarlos
 * @since 2022-04-05 14:34
 */
public abstract class Controller {

    @Autowired
    ApplicationContext context;

    public JsonElement toJsonTree(Object object) {
        return gson().toJsonTree(object);
    }

    public JsonElement toJson(Object object) {
        return toJsonTree(object);
    }

    public <T> JsonElement asJson(String name, Class<T> tClass) {
        return toJsonTree(getBean(name, tClass));
    }

    public <T> JsonElement asJson(Class<T> tClass) {
        return toJsonTree(getBean(tClass));
    }

    public JsonElement asJson(String name) {
        return toJsonTree(getBean(name));
    }

    public <T> T getBean(String name, Class<T> tClass) {
        return context.getBean(name, tClass);
    }

    public <T> T getBean(Class<T> tClass) {
        return context.getBean(tClass);
    }

    public Object getBean(String name) {
        return context.getBean(name);
    }

    public Gson gson() {
        return context.getBean(Gson.class);
    }

}
